package com.lsh.mq;

import com.lsh.util.aliyun.sms.AlySmsUtil;
import com.lsh.util.result.Result;
import net.sf.json.JSONObject;
import org.apache.activemq.command.ActiveMQMapMessage;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import java.util.Map;

/**
 * 手机号码注册验证码
 * 发送注册验证码至指定手机号码
 * @author Administrator
 * @date 2019/11/12 1:14
 */
public class PhoneRegisterConsumer implements MessageListener {

    private Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    AlySmsUtil alySmsUtil;

    @Override
    public void onMessage(Message message) {
        String phone = null;
        String code = null;
        try {
            Map<String, Object> contentMap = ((ActiveMQMapMessage) message).getContentMap();
            phone = contentMap.get("phone").toString();
            code = contentMap.get("code").toString();
            String resultResponse = alySmsUtil.sendSms(phone, code);
            JSONObject jsonObject = JSONObject.fromObject(resultResponse);
            String responseCode = (String) jsonObject.get("code");
            if ("OK".equalsIgnoreCase(responseCode)) {
                logger.info("向手机号码为:" + phone + ",发送的验证码为" + code + "，响应结果为:" + resultResponse);
            }else {
                logger.error("向手机号码为:" + phone + ",发送的验证码为" + code + "，响应结果为:" + resultResponse);
            }
        } catch (JMSException e) {
            e.printStackTrace();
            logger.error("向手机号码为:" + phone + ",发送的验证码为" + code + "，过程中断..异常为:" + e.getStackTrace());
        }
    }
}
